const { ccclass, property } = cc._decorator;

import n from "./EventMgr"
import l from "./MatchGameEventType"
import s from "./frameworkManager"
import c from "./EngineUtil"
import p from "./MatchAniCtrl"
import h from "./MatchGameCenterCtrl"
import f from "./MatchGameUtilsDataMgr"
import g from "./MatchGameConfig"
import y, { LayerType } from "./ObstacleConfig"
import m from "./MatchGameAudioMgr"
import _ from "./MatchGameDataMgr"
import v, { SquareStateMachineState } from "./SquareStatusMgr";
import MathUtils from "./MathUtil";
import MatchGameConfig from "./MatchGameConfig";
import { GuideElimitateType } from "./MatchGameGuidePageCtrl";

export enum SquareStatu {
    Normal = 1,
    Dowing = 2,
    Destory = 4,
    ReadyDestory = 5,
    Changing = 6
}

@ccclass
export default class SquareItem extends cc.Component {

    public _targetId = null;
    public _pos = null;
    public canMove = true;
    public readyChangeProp = false;
    public isSelectProp = false;
    public isPropllerMark = false;
    public queueAction = [];
    public moveTargetPos = null;
    public layerType = LayerType.Middle;
    public tweenPromis = null;
    public guideElimitateType;
    public propellerMarkCount = 0;
    public squareFsm = v.getSquareFsm();
    public isCanTweenMove = false;
    public isHide = false;
    public curMoveTimes = 0;
    public targetPos = null;
    public movePath = [];
    public duringMovePath = [];
    public realMovePosArr = [];
    public downDuringDowing = false;
    public isMaxMoveItem = false;
    public blockY = -1;
    public moveGuideTween = null;
    public lightGuideTween = null;

    public squareItemInfo;
    public squareType;
    public itemType;


    public init(e, t, a, o, i) {
        this._row = e;
        this._col = t;
        this.squareItemInfo = i || null;
        this.squareType = a;
        this.itemType = o;
        this.node.setPosition(f.getPos(e, t));
        this.node.opacity = 255;
        this.readyChangeProp = false;
        this.tweenPromis = null;
        this.isSelectProp = false;
        v.normalSquare(this);
        this.isPropllerMark = false;
        this.node.name = "row_col:" + e + t + "-" + a + "-" + o;
        this.guideElimitateType = GuideElimitateType.Null;
        this.propellerMarkCount = 0;
        this.duringMovePath = [];
        this.downDuringDowing = false;
        this.isMaxMoveItem = false;
        this.isHide = false;
    };
    public setGuideElimitateType(e) {
        this.guideElimitateType = e;
    };
    public _row;
    public _col;
    public setRowCol(e, t) {
        this._row = e;
        this._col = t;
    };
    public setPosition(e, t) {
        var i = this.getSpineNode();
        i.x = i.y = 0;
        this.node.setPosition(f.getPos(e, t));
    };
    public moveDown() {
        this.canMove;
    };
    public moveTo(...arg) {
        s.log("moveto?????");
    };
    public destorySelf() {
        this.node.stopAllActions();
        this.movePath = [];
        v.destorySquare(this);
    };
    public initMovePosArr() { };
    public clearMovePosArr() { };
    public getMovePosArr() {
        return this.movePath;
    };
    public addMovePos(e, t, a?) {
        a && a > 0 && (this.blockY = a);
        if (!v.getSquareDropStatus(this)) {
            for (var o = this.movePath.length - 1; o >= 0; o--) if (t <= this.movePath[o].col) return;
            this.movePath.push({
                row: e,
                col: t
            });
            v.getSquareDropStatus(this) && s.log("下落过程中添加路径22==", c.clone(this.movePath));
        }
    };
    public getMoveTime() { };
    public getRealMovePath() {
        if (this.movePath.length) {
            var e = [];
            this.movePath.sort(function (e, t) {
                return e.col - t.col;
            });
            for (var t = 0; t < this.movePath.length; t++) this.movePath[t + 1] && this.movePath[t + 1].row == this.movePath[t].row && this.movePath[t - 1] && this.movePath[t - 1].row == this.movePath[t].row || e.push({
                row: this.movePath[t].row,
                col: this.movePath[t].col
            });
            return e;
        }
    };
    public setIsMaxItem() {
        this.isMaxMoveItem = true;
    };
    public tweenMove(e) {
        var t = this;
        if (!v.getSquareNormalStatus(this)) {
            s.error("tweenMove==11");
            return 0;
        }
        this.addMovePos(this._row, this._col);
        if (!this.movePath.length) {
            s.error("tweenMove==22");
            return 0;
        }
        this.realMovePosArr = this.getRealMovePath();
        this.movePath = [];
        s.log("this.realMovePosArr==", c.clone(this.realMovePosArr));
        if (!this.realMovePosArr.length) {
            s.error("tweenMove==33");
            return 0;
        }
        if (1 == this.realMovePosArr.length) {
            s.error("tweenMove==44");
            this.realMovePosArr.push({
                row: this._row,
                col: this._col
            });
        }
        v.dropSquare(this);
        var a = 1, o = this.realMovePosArr[a - 1], i = this.realMovePosArr[a];
        this.node.zIndex = 999;
        var r = ()=> {
            if ((a += 1) != t.realMovePosArr.length) {
                o = t.realMovePosArr[a - 1];
                i = t.realMovePosArr[a];
                t.moveTween(o, i, r, t.realMovePosArr.length == a + 1, false);
            } else t.onTweenMoveFinish();
        };
        this.moveTween(o, i, r, this.realMovePosArr.length == a + 1, true);
        return this.getAllTime(this.realMovePosArr);
    };
    public onTweenMoveFinish() {
        if (this.isMaxMoveItem) {
            n.trigger(l.BIRD_FINISH_FALL_DOWN, {
                row: this._row
            });
            this.isMaxMoveItem = false;
            _.checkCanFill() ? _.AllFill() : _.getAllDowingSquare().length || _.checkNoElimate();
        }
        s.log("运动的轨迹==", this.node.name, "轨迹==", this.realMovePosArr);
        s.log("运动的棋子==", this._row, this._col);
        this.realMovePosArr = [];
        v.dropSquareFinish(this);
        this.blockY = -1;
        this.setMiddleParent();
        this.node.zIndex = 0;
        this.downDuringDowing = false;
        _.onLastSquareMoveDown();
    };
    public deepCloneMovePath(e) {
        for (var t = [], a = 0; a < e.length; a++) e[a] && null != e[a].row && null != e[a].col && t.push({
            row: e[a].row,
            col: e[a].col
        });
        return t;
    };
    public moveTween(e, t, a, o, i?) {
        var r = this;
        void 0 === i && (i = false);
        if (e && t) {
            var n = .11 * (t.col - e.col), l = f.getV3Pos(t.row, t.col), s = e.row - t.row, c = l, u = this.getDelayTime(e, t);
            i ? cc.tween(this.node).delay(u).to(n, {
                position: c
            }, {
                easing:  (e) => {
                    return 0 === e || 1 === e ? e : -1 * Math.cos(e * Math.PI / 2 * 1) + 1;
                }
            }).call(function () {
                m.playSquareDownAudio(s);
                a && a();
                o && r.playBounceAni();
            }).start() : cc.tween(this.node).to(n, {
                position: c
            }, {
                easing:  (e) => {
                    return 0 === e || 1 === e ? e : -1 * Math.cos(e * Math.PI / 2 * 1) + 1;
                }
            }).call(function () {
                m.playSquareDownAudio(s);
                a && a();
                o && r.playBounceAni();
            }).start();
        }
    };
    public getAllTime(e) {
        var t = e[0], a = e[e.length - 1];
        return this.getDelayTime(e[0], e[1]) + .11 * (a.col - t.col);
    };
    public getOneMoveTime(e, t) {
        var a = 0, o = e.row - t.row;
        a = .04 + .08 * (t.col - e.col);
        var i = this.getDelayTime(e, t);
        0 != o && (i = 0);
        return a + i;
    };
    public getDelayTime(e, t) {
        var a = 0, o = (e.row, t.row, h.dropInfo), i = 0, r = 0;
        if (o) {
            i = t.col - e.col;
            r = o[t.row].endIndexY - t.col;
            a = e.row % 2 == 0 ? .025 * (i + r) : .022 * (i + r);
        }
        return a;
    };
    public update() {
        v.getSquareDropStatus(this) && this.node && (this.node.y > -this.blockY * MatchGameConfig.ITEM_HEIGHT ? this.node.opacity = 0 : this.node.opacity = 255);
        v.getSquareNormalStatus(this) && 255 != this.node.opacity && (this.node.opacity = 255);
    };
    public shuffMove() {
        var e = this.node.position, t = f.getV3Pos(this._row, this._col), a = MathUtils.getDistance(e, t) / 800;
        a = Math.max(.5, a);
        if (0 != (a = Math.min(a, 4))) {
            cc.tween(this.node).to(a, {
                position: t
            }).call(function () { }).start();
            return a;
        }
    };
    public getShuffTime() { };
    public playMoveGuideAni(e) {
        var t, a, o = this;
        if (0 == e || 2 == e) {
            t = 0;
            a = 0 == e ? -10 : 10;
        } else {
            t = 1 == e ? -10 : 10;
            a = 0;
        }
        var i = this.getSpineNode();
        this.moveGuideTween = cc.tween(i).call(function () {
            o.squareType == g.SquareType.MatchItem && p.playBreathAni(i.getComponent(sp.Skeleton));
        }).by(.2, {
            x: t,
            y: a
        }).by(.2, {
            x: -t,
            y: -a
        }).by(.2, {
            x: t,
            y: a
        }).by(.2, {
            x: -t,
            y: -a
        }).call(function () {
            i.x = i.y = 0;
            o.squareType == g.SquareType.MatchItem && p.playIdleAni(i.getComponent(sp.Skeleton));
        }).delay(.4).call(function () {
            o.moveGuideTween.start();
        });
        this.moveGuideTween.start();
    };
    public playLightGuideAni() {
        var e = this, t = this.getSpineNode();
        if (t) {
            this.squareType == g.SquareType.MatchItem && this.node && p.playLightingAni(t.getComponent(sp.Skeleton));
            this.lightGuideTween = cc.tween(t).call(function () {
                e.squareType == g.SquareType.MatchItem && p.playBreathAni(t.getComponent(sp.Skeleton));
            }).delay(.2).delay(.2).delay(.2).delay(.2).call(function () {
                e.squareType == g.SquareType.MatchItem && p.playBreathAni(t.getComponent(sp.Skeleton));
            }).delay(.4).call(function () {
                e.lightGuideTween.start();
            });
            this.lightGuideTween.start();
        }
    };
    public setGuideNormal() {
        if (this.node) {
            if (this.moveGuideTween) {
                this.moveGuideTween.stop();
                // (e = this.getSpineNode()) //&& e.setPosition(37.5, -37.5);
                var i = this.getSpineNode();
                i.x = i.y = 0;
            }
            this.lightGuideTween && this.lightGuideTween.stop();
            if (this.squareType == g.SquareType.MatchItem && this.node) {
                var e;
                (e = this.getSpineNode()) && p.playIdleAni(e.getComponent(sp.Skeleton));
            }
        }
    };
    public touchStartEvent() { };
    public setMiddleParent() {
        this.node.parent == h.middleMaskParents && (this.node.parent = h.middleParents);
    };
    public addPropellerMark() {
        this.propellerMarkCount += 1;
    };
    public getPropellerMarkItem() {
        return v.getSquareReadyDestoryStatus(this) || v.getSquareDestoryStatus(this) ? null : this.squareType == g.SquareType.MatchItem || this.squareType == g.SquareType.PropItem ? this : this.node.getComponent("ObstaclePrefabCtrl") ? this.squareItemInfo.Hp < 0 ? this : this.squareItemInfo.Hp - this.propellerMarkCount > 0 ? this : null : this.node.getComponent("EmptyObstacleItemCtrl") ? this.node.getComponent("EmptyObstacleItemCtrl").pointerObstacle.propplerMarkItem() : void 0;
    };
    public delatePropplerMark() {
        this.propellerMarkCount <= 0 ? s.error("不能删除小飞机标记", this) : this.propellerMarkCount -= 1;
    };
    public playBounceAni() {
        var e = c.GetChildByName(this.node, "aniNode", true);
        if (e && e.getComponent(cc.Animation)) {
            e.getComponent(cc.Animation).stop();
            e.getComponent(cc.Animation).play();
        }
    };
    public getSpineNode() {
        return c.GetChildByName(this.node, "spine", true);
    };
    public getLightSpine() {
        return c.GetChildByName(this.node, "lightSpine", true);
    };
    public hideItem() {
        this.node.stopAllActions();
        this.movePath = [];
        this.isHide = true;
        v.gotoState(this, SquareStateMachineState.readyDestroy);
    };
}
